import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'index',
    component: () => import('../views/home.vue'),
    //在根目录下切换路由
    children: [
      // 用户列表
      {
        path: 'users',
        name: 'users',
        component: () => import('../views/users.vue')
      },
      // 权限列表
      {
        path: 'rights',
        name: 'rights',
        component: () => import('../views/rights.vue')
      },
      // 角色列表
      {
        path: 'roles',
        name: 'roles',
        component: () => import('../views/roles.vue')
      },
      //商品列表
      {
        path: 'goods',
        name: 'goods',
        component: () => import('../views/goods.vue')
      },
      // 商品分类
      {
        path: 'categories',
        name: 'categories',
        component: () => import('../views/categories.vue')
      },
      // 订单列表
      {
        path: 'orders',
        name: 'orders',
        component: () => import('../views/orders.vue')
      },
      // 分类参数
      {
        path: 'params',
        name: 'params',
        component: () => import('../views/params.vue')
      },
      // 数据统计
      {
        path: 'reports',
        name: 'reports',
        component: () => import('../views/reports.vue')
      },
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login.vue')
  },
  {
    path: '/home',
    name: 'home',
    component: () => import('../views/home.vue')
  }
]

const router = new VueRouter({
  routes
})

//导航守卫
router.beforeEach((to, from, next) => {
  if (to.path == '/home' || to.path == '/') {
    if (!localStorage.getItem('userinfo')) {
      next('/login')
    }
  }
  next()
})

export default router
